package com.chao.yyks.modular.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chao.common.bean.IUser;
import com.chao.common.util.ListUtile;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.common.util.redis.SysRedis;
import com.chao.yyks.modular.sys.bean.EnStudent;
import com.chao.yyks.modular.sys.bean.EnTeacher;
import com.chao.yyks.modular.sys.bean.LoginUser;
import com.chao.yyks.modular.sys.mapper.EnTeacherMapper;
import com.chao.yyks.modular.sys.service.EnTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.chao.common.util.reaction.PageList;
import java.util.Date;
import java.util.List;

import static com.chao.common.util.SysConstant.TOKEN;


/**
 * 教师ServiceImpl
 * chao
 * 2024-09-09
 */
@Service
public class EnTeacherServiceImpl implements EnTeacherService {

	/**
	 * 教师Mapper
	 */
	@Autowired
	private EnTeacherMapper enTeacherMapper;

	@Override
	public LoginUser teacherLogin(EnTeacher enTeacher) {
		JSONObject json = new JSONObject();
		json.put("loginId", enTeacher.getTeacherMobile());
		json.put("teacherPassword", StrUtile.getMD5(enTeacher.getTeacherPassword()));
		List<EnTeacher> userList = enTeacherMapper.selectEnTeacherByList(json).getList();
		if(ListUtile.isEmpty(userList)){
			throw new SysException("用户名密码错误！");
		}
		EnTeacher teacher = userList.get(0);
		if(teacher.getIsDel() == 1){
			throw new SysException("账号已禁用，无法登录！");
		}

		IUser user = new IUser();
		user.setUserId(teacher.getTeacherId());
		user.setUserName(teacher.getTeacherName());
		String token=StrUtile.getToken(user.getUserId(),StrUtile.getUUID());

		//将当前登录用户缓存到redis,设置token有效期为 108000 秒(30小时)
		SysRedis.setValue(TOKEN+":"+token, JSON.toJSON(user), 108000);

		//记录日志
		return new LoginUser(token,user);
	}

	/**
	 * 新增教师
	 */
	@Override
	@Transactional
	public void insertEnTeacher(EnTeacher enTeacher){
		enTeacher.setTeacherId(StrUtile.getId());
		enTeacher.setCtime(new Date());
		enTeacher.setCreateUserId(RequestUtile.getLoginUserId());
		String mobile = enTeacher.getTeacherMobile();
		if(StrUtile.isEmpty(enTeacher.getTeacherMobile())){
			mobile = "123456";
		}
		enTeacher.setTeacherPassword(StrUtile.getMD5(mobile.substring(mobile.length() - 6)));
		enTeacherMapper.insertEnTeacher(enTeacher);
	}

	/**
	 * 修改教师
	 */
	@Override
	@Transactional
	public void updateEnTeacher(EnTeacher enTeacher){
		EnTeacher enTeacherPojo = enTeacherMapper.selectEnTeacherByTeacherId(enTeacher.getTeacherId());
		if(null == enTeacherPojo){
		    throw new SysException("修改教师失败:记录不存在!");
		}
		enTeacherMapper.updateEnTeacher(enTeacher);
	}

	/**
	 * 删除教师
	 */
	@Override
	@Transactional
	public void deleteEnTeacher(String teacherId){
		EnTeacher enTeacher = enTeacherMapper.selectEnTeacherByTeacherId(teacherId);
		if(null == enTeacher){
		    throw new SysException("删除教师失败:记录不存在!");
		}
		enTeacherMapper.deleteEnTeacher(teacherId);
	}

	/**
	 * 修改用户密码
	 */
	@Override
	public void updatePassword(JSONObject json){
		String oldPassword = json.getString("oldPassword");
		EnTeacher enTeacher = enTeacherMapper.selectEnTeacherByTeacherId(RequestUtile.getLoginUserId());
		//校验密码是否正确
		if(!enTeacher.getTeacherPassword().equals(StrUtile.getMD5(oldPassword))){
			throw new SysException("请输入正确的当前密码后再进行修改！");
		}
		String newPassword = json.getString("newPassword");
		enTeacherMapper.updatePassword(RequestUtile.getLoginUserId(),StrUtile.getMD5(newPassword));
	}

	/**
	 * 重置用户密码
	 */
	@Override
	public void resetPassword(String teacherId){
		EnTeacher enTeacher = enTeacherMapper.selectEnTeacherByTeacherId(teacherId);
		if(null == enTeacher){
			throw new SysException("重置用户密码失败:教师不存在!");
		}
		enTeacherMapper.updatePassword(teacherId,StrUtile.getMD5(enTeacher.getTeacherMobile().substring(enTeacher.getTeacherMobile().length()-6)));
	}


	/**
	 * 查询教师详情
	 */
	@Override
	public EnTeacher selectEnTeacherByTeacherId(String teacherId){
		return enTeacherMapper.selectEnTeacherByTeacherId(teacherId);
	}

	/**
	 * 查询教师列表
	 */
	@Override
	public PageList<EnTeacher> selectEnTeacherByList(JSONObject json){
		return enTeacherMapper.selectEnTeacherByList(json);
	}

	/**
	 * 分页查询教师列表
	 */
	@Override
	public PageList<EnTeacher> selectEnTeacherByPage(JSONObject json){
		return enTeacherMapper.selectEnTeacherByList(StrUtile.getPageJson(json));
	}

}